/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.gdp;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.Comparator;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;

import twaver.Element;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.chart.BarChart;
import twaver.chart.PieChart;
import twaver.table.TElementTable;
import twaver.table.TTableAdapter;
import twaver.table.TTableModelEvent;
import demo.DemoPane;

public class GDPDemo extends DemoPane{
	
	private TDataBox box = new TDataBox();
	private PieChart pie = new PieChart(box);
	private BarChart bar = new BarChart(box);
	private TElementTable table = new TElementTable(box);
	
	public GDPDemo(){
		new MouseListener(pie);
		new MouseListener(bar);
		pie.setLegendOrientation(TWaverConst.LABEL_ORIENTATION_RIGHT);
		bar.setLegendOrientation(TWaverConst.LABEL_ORIENTATION_RIGHT);
		pie.setValueTextVisible(false);
		bar.setValueTextVisible(false);
		
		bar.setEnableYZoom(false);
		bar.setEnableYTranslate(false);
		
		GridLayout layout = new GridLayout(2, 1);
		layout.setVgap(20);
        JPanel chartPane = new JPanel(layout);  
        chartPane.add(pie);
        chartPane.add(bar);
        
        JPanel tablePane = new JPanel(new BorderLayout());
        tablePane.add(new Controlbar(table), BorderLayout.SOUTH);
        tablePane.add(new JScrollPane(table), BorderLayout.CENTER);
        
		JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, chartPane, tablePane);
		split.setDividerLocation(400);
		this.add(split);
        
		table.setMultiColumnSortable(false);
        table.setIteratorByHiberarchy(true);
        table.setElementClass(Node.class);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.registerElementClassXML(Node.class, "/demo/chart/gdp/GDP.xml");
        
		final Comparator comparator = new SortComparator(table);
		pie.setSortComparator(comparator);
		bar.setSortComparator(comparator);
		table.addTableListener(new TTableAdapter(){
		    public void tableDataChanged(TTableModelEvent e) {
		    	if(e.getType() == TTableModelEvent.SORT_COLUMN){
		    		List sortColumns = table.getSortColumnList();
		    		if(sortColumns.size() == 0){
		    			pie.setSortComparator(null);
		    			bar.setSortComparator(null);
		    		}else{
		    			pie.setSortComparator(comparator);
		    			bar.setSortComparator(comparator);
		    		}
		    	}
		    }
		});

		addCountry("Greece", 225206300000d);  
		addCountry("Ireland", 201816900000d); 
		addCountry("Netherlands", 624202200000d); 
		addCountry("Belgium", 370824300000d);   
		addCountry("Switzerland", 367029400000d);  
		addCountry("Turkey", 362501700000d); 
		addCountry("Sweden", 357682600000d);  
		addCountry("Saudi Arabia", 309778500000d); 
		addCountry("Austria", 306072900000d); 
		addCountry("Spain", 1124640000000d);  
		addCountry("Korea, South", 787624500000d); 
		addCountry("Mexico", 768437500000d); 
		addCountry("Russia", 763720000000d); 
		addCountry("China", 2234297000000d); 
		addCountry("United Kingdom", 2198789000000d);  
		addCountry("Australia", 732499200000d); 
		addCountry("United States", 12416510000000d); 
		addCountry("Canada", 1113810000000d);  
		addCountry("India", 805713800000d); 
		addCountry("Brazil", 796055200000d);  
		addCountry("Norway", 295512800000d); 
		addCountry("Indonesia", 287216800000d); 
		addCountry("Denmark", 258714400000d);  
		addCountry("South Africa", 239543200000d);  
		addCountry("Finland", 193160100000d); 
		addCountry("Poland", 303228600000d); 
		addCountry("Japan", 4533965000000d);  
		addCountry("Germany", 2794926000000d); 
		addCountry("France", 2126630000000d); 
		addCountry("Italy", 1762519000000d); 
	}
	
	private void addCountry(String country, double dollars){
		Element element = new Node();
		element.setName(country);
		element.setVisible(TWaverUtil.getRandomBool());
		element.putChartValue(dollars);
		element.putChartColor(TWaverUtil.getRandomColor());
		box.addElement(element);
	}
}